<?php
namespace app\admin\controller;
use think\Request;
use think\Session;
use app\common\controller\Backend;
use app\common\model\BackendUserGroupModel;
use app\common\model\BackendMenuModel;
use app\common\model\BackendRuleModel;

class UserGroup extends Backend
{

  public function __construct(){
    parent::__construct();
  }

  /*首页*/
  public function index(Request $request = null){
    $UserGroupModel = new BackendUserGroupModel();
    $list = $UserGroupModel->getUserGroupList($request->get());

    return view('index',[
      'list' => $list,
    ]);
  }

  /*添加页面*/
  public function create(Request $request = null){
    if($request->isPost()){
      $post = $request->post();
      // var_dump($post);die();
      $BackendUserGroupModel = new BackendUserGroupModel();
      $result = $BackendUserGroupModel->allowField(true)->validate(true)->save($post);

      if(false === $result){
         $this->error($BackendUserGroupModel->getError());
      }else{
        if(!empty($post['menu_id'])){
          //添加权限
          $BackendRuleModel = new BackendRuleModel();
          $BackendRuleModel->addRule($post, $BackendUserGroupModel->id);
        }

        $this->success("成功！");
      }
      
    }else{
      //获取菜单列表
      $menuList = (new BackendMenuModel())->getMenuList();

      return view('create',[
        'menu_list'=> $menuList,
      ]);
    }
  }

  /*编辑页面*/
  public function update(Request $request = null){
    if($request->isPost()){
      $post = $request->post();
      $BackendUserGroupModel = new BackendUserGroupModel();
      $result = $BackendUserGroupModel->allowField(true)->validate(true)->save($post, $post['id']);

      if(false === $result){
          // 验证失败 输出错误信息
          $this->error($BackendUserGroupModel->getError());
      }else{
        //修改权限
        $BackendRuleModel = new BackendRuleModel();
        $BackendRuleModel->editRule($post, $post['id']);

        $this->success("成功！");
      }
    }else{
      $id = $request->get('id/d');
      $BackendUserGroupModel = new BackendUserGroupModel();
      //获取权限组信息
      $userGroupModel = $BackendUserGroupModel->getUserGroupById($id);
      //获取权限信息
      $BackendRuleModel = new BackendRuleModel();
      $menuList = $BackendRuleModel->getMneuRuleByGroupId($id);

      return view('update',[ 'userGroupModel' => $userGroupModel,'menuList'=>$menuList]);
    }
  }

  /*删除*/
  public function deleteall(){
    $BackendUserGroupModel = new BackendUserGroupModel();
    if($BackendUserGroupModel->deleteAll($_POST['id']) > 0){
      return json(['status'=>200,'msg'=>'成功']);
    }else{
      return json(['status'=>400,'msg'=>'失败']);
    }
  }


  //全局禁用页面
  public function forbidden(Request $request = null){
    if($request->isPost()){
      $post = $request->post();
      $BackendUserGroupModel = new BackendUserGroupModel();
      $result = $BackendUserGroupModel->allowField(true)->validate(true)->save($post, $post['id']);

      if(false === $result){
          // 验证失败 输出错误信息
          $this->error($BackendUserGroupModel->getError());
      }else{
        //修改权限
        $BackendRuleModel = new BackendRuleModel();
        $BackendRuleModel->editRule($post, $post['id']);

        $this->success("成功！");
      }
    }else{

      return view('forbidden');
    }
  }
}
